//
//  YZTableViewController.swift
//  SwiftTest
//
//  Created by Linyongzhi on 16/4/27.
//  Copyright © 2016年 Uama. All rights reserved.
//  测试UISearchController的用法

import UIKit

@available(iOS 8.0, *)
class YZTableViewController: UITableViewController {
    
    private var searchViewController: UISearchController!
    private var resultViewController: YZSearchResultTableViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.resultViewController = YZSearchResultTableViewController(style: UITableViewStyle.Grouped)
        
        self.searchViewController = UISearchController(searchResultsController: self.resultViewController)
        self.searchViewController.searchResultsUpdater = self
        self.tableView.tableHeaderView = self.searchViewController.searchBar
        
        self.tableView.registerNib(UINib(nibName: "YZTableViewCell", bundle: nil), forCellReuseIdentifier: YZTableViewCell.kReUsedIdentifier)
        
//        self.tableView.registerClass(YZTableViewCell.self, forCellReuseIdentifier: YZTableViewCell.kReUsedIdentifier)

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(YZTableViewCell.kReUsedIdentifier, forIndexPath: indexPath)

        cell.textLabel?.text = "杭州市"

        return cell
    }
}

@available(iOS 8.0, *)
extension YZTableViewController: UISearchResultsUpdating {
    
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        // 在这里更新搜索结果页面的数据
    }
}
